﻿/***********************************************************************
 * <copyright file="SqlServerRoleInSiteDao.cs" company="B/S JSC">
 * -->    Copyright (C) statement. All right reserved
 * </copyright>
 * 
 * Created:   Hoang Bich Son 
 * Email:    sonhb@newman.vn
 * Website:
 * Create Date: Monday, March 31, 2014
 * Usage: 
 * 
 * RevisionHistory: 
 * Date         Author               Description 
 * 
 * ************************************************************************/

using System;
using System.Collections.Generic;
using System.Data;
using BusinessSecurity;
using DataObjects;
using DataObjects.ADO;
using DataSecurity.ADO.SqlServer.DataContext;
using DataSecurity.StandartInterfaces;

namespace DataSecurity.ADO.SqlServer.Dictionary
{
    public class SqlServerRoleInSiteDao : IRoleInSite
    {
        /// <summary>
        /// Gets the specified identifier.
        /// </summary>
        /// <param name="id">The identifier.</param>
        /// <returns></returns>
        public RoleInSite Get(int id)
        {
            const string sql = @"uspGet_RoleInSite_ByID";
            object[] parms = { "@RoleInSiteId", id };
            return Db.Read(sql, CommandType.StoredProcedure, Make, parms);
        }
        /// <summary>
        /// Gets the list.
        /// </summary>
        /// <returns></returns>
        public List<RoleInSite> GetListByRole(int roleId)
        {
            const string sql = @"uspGetAll_RoleInSite";
            object[] parms = { "@WhereClause", "RoleId = " + roleId };
            return Db.ReadList(sql, CommandType.StoredProcedure, Make, parms);
        }
        public List<RoleInSite> GetListBySite(int siteId)
        {
            const string sql = @"uspRoleInSite_GetListBySiteID";
            object[] parms = { "@SiteId",  siteId };
            return Db.ReadList(sql, CommandType.StoredProcedure, Make, parms);
        }
        /// <summary>
        /// Gets the paged.
        /// </summary>
        /// <param name="currentPage">The current page.</param>
        /// <returns></returns>
        public List<RoleInSite> GetPaged(int currentPage)
        {
            const string sql = @"uspGetPaged_RoleInSites";
            object[] parms = { "@WhereClause", null, "@OrderBy", null, "@PageIndex", currentPage };
            return Db.ReadList(sql, CommandType.StoredProcedure, Make, parms);
        }

        /// <summary>
        /// Inserts the specified RoleInSite.
        /// </summary>
        /// <param name="roleId">The role identifier.</param>
        /// <param name="objs">The objs.</param>
        /// <returns></returns>
        /// <exception cref="System.NotImplementedException"></exception>
        public object Insert(int roleId, List<RoleInSite> objs)
        {
            const string sql = @"uspRoleInSite_Insert";
            return Db.Insert(sql, CommandType.StoredProcedure, Take(objs));
        }

        /// <summary>
        /// Updates the specified RoleInSite.
        /// </summary>
        /// <param name="objs">The objs.</param>
        /// <returns></returns>
        /// <exception cref="System.NotImplementedException"></exception>
        public object Update(List<RoleInSite> objs)
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// Deletes the specified RoleInSite.
        /// </summary>
        /// <param name="roleId">The role identifier.</param>
        /// <returns></returns>
        /// <exception cref="System.NotImplementedException"></exception>
        public object Delete(int roleId)
        {
            const string sql = @"uspRoleInSite_DeleteByRoleID";
            object[] parms = { "@RoleId", roleId };
            return Db.Delete(sql, CommandType.StoredProcedure, parms);
        }
        /// <summary>
        /// The make
        /// </summary>
        private static readonly Func<IDataReader, RoleInSite> Make = reader =>
          new RoleInSite
          {              
              RoleId = reader["RoleId"].AsId(),
              SiteId = reader["SiteId"].AsId(),
              PermissionId = reader["PermissionId"].AsId()
          };

        /// <summary>
        /// Creates query parameters list from UserGroup object
        /// </summary>
        /// <param name="item">The item.</param>
        /// <returns>
        /// Name value parameter list.
        /// </returns>
        private static object[] Take(RoleInSite item)
        {
            return new object[]  
            {
                "@RoleId", item.RoleId,
                "@SiteId",item.SiteId,
                "@PermissionId", item.PermissionId
            };
        }
        private static object[] Take(List<RoleInSite> items)
        {
            return new object[]  
            {
                "@RoleInSite", Mapper.ToDataContext(items).ToDataTable()
            };
        }
    }
}
